package com.start.pay.core.log;

import com.start.pay.core.model.StartBasePayCallLog;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class StartPayLogManage {
    private static final ExecutorService executorService = Executors.newFixedThreadPool(5);
    private static StartPayLogManage startPayLogManage = new StartPayLogManage();

    private HashMap<String, StartPayLogHandle> logServiceHashMap = new HashMap<>();

    public static StartPayLogManage getInstance() {
        return startPayLogManage;
    }

    public <T extends StartBasePayCallLog> void registerLogHandle(Class<T> clazz, StartPayLogHandle<? extends T> handle) {
        if (logServiceHashMap.containsKey(clazz.getName())) {
            return;
        }
        logServiceHashMap.put(clazz.getName(), handle);
    }

    private StartPayLogManage() {

    }


    public static void log(StartBasePayCallLog log) {
        startPayLogManage.writeLog(log);
    }

    public void writeLog(StartBasePayCallLog logModel) {
        if (logModel == null) return;
        executorService.execute(() -> {
            String name = logModel.getClass().getName();
            if (!logServiceHashMap.containsKey(name)) {
                log.warn("未找到类型为：" + name + " 的日志处理器");
                return;
            }
            StartPayLogHandle startPayLogHandle = logServiceHashMap.get(name);
            startPayLogHandle.handler(logModel);
        });
    }
}
